import pytest
import requests
from send.Send import Send
from utils.read_yaml import read_yaml
from send.DButils import select
from send.DButils import update


class Test_login:
    @pytest.mark.parametrize("args",read_yaml("../data/login.yaml"))
    def test_login(self,args):
        session = requests.session()
        name= args["username"]
        password= args["password"]
        exp = args["exp"]

        send = Send()

        result= send.send_login(session,name,password)

        assert result["code"]== 200 and result["body"]["code"]== exp

class Test_register:
    @pytest.mark.parametrize("args",read_yaml("../data/register.yaml"))
    def test_register(self,args):
        session=requests.session()
        name= args["username"]
        password = args["password"]
        exp= args["exp"]

        send = Send()

        result=send.send_register(session, name, password)

        assert result["code"] == 200 and result["body"]["code"] == exp

class Test_add:
    @pytest.mark.parametrize("args", read_yaml("../data/addbank.yaml"))
    def test_add(self,args):
        session=requests.session()

        name=args["username"]
        password=args["password"]

        cardbank=args["cardbank"]
        cardtype=args["type"]
        cardnum=args["cardnum"]
        exp=args["exp"]

        send=Send()
        send.send_login(session,name,password)

        result=send.send_add(session,cardbank,cardtype,cardnum)
        assert result["code"] == 200 and result["body"]["code"] == exp

class Test_update:
    @pytest.mark.parametrize("args", read_yaml("../data/updatebank.yaml"))
    def test_update(self,args):
        session=requests.session()
        select_sql="select id from bankcard where userId=%s and defaultl=0"
        id=select(select_sql,['1'])[0][0]
        name = args["username"]
        password = args["password"]

        cardbank = args["cardbank"]
        cardtype = args["type"]
        cardnum = args["cardnum"]
        exp = args["exp"]

        send=Send()

        send.send_login(session, name, password)

        result=send.send_update(session,id, cardbank, cardtype, cardnum)
        assert result["code"] == 200 and result["body"]["code"] == exp

class Test_delete:
    @pytest.mark.parametrize("args", read_yaml("../data/deletebank.yaml"))
    def test_delete(self,args):
        session=requests.session()
        select_sql = "select id from bankcard where userId=%s and defaultl=0"
        id = select(select_sql, ['1'])[0][0]
        name = args["username"]
        password = args["password"]
        exp=args["exp"]

        send=Send()

        send.send_login(session, name, password)

        result=send.send_delete(session, id)

        assert result["code"]==200 and result["body"]["code"] == exp

class Test_updatePwd:
    @pytest.mark.parametrize("args", read_yaml("../data/updatePwd.yaml"))
    def test_updatePwd(self,args):
        session=requests.session()

        name = args["username"]
        password = args["password"]

        id =args["id"]
        oldpwd=args["oldpwd"]
        newpwd=args["newpwd"]
        exp=args["exp"]

        send=Send()

        send.send_login(session, name, password)

        result=send.send_updatePwd(session, id, oldpwd, newpwd)
        update_sql = "update user set password=%s where id=1"
        update(update_sql, ["e10adc3949ba59abbe56e057f20f883e"])
        assert result["code"]==200 and result["body"]["code"]==exp

class Test_buyc:
    @pytest.mark.parametrize("args", read_yaml("../data/buyc.yaml"))
    def test_buyc(self,args):
        session=requests.session()

        name = args["username"]
        password = args["password"]

        cid=args["changeMoneyId"]
        uid=args["userId"]
        pwd=args["passWord"]
        exp = args["exp"]

        send=Send()
        send.send_login(session, name, password)

        result=send.send_buyc(session,cid,uid,pwd)

        assert result["code"]==200 and result["body"]["code"] == exp


class Test_buyt:
    @pytest.mark.parametrize("args", read_yaml("../data/buyt.yaml"))
    def test_buyc(self, args):
        session = requests.session()

        name = args["username"]
        password = args["password"]

        tid = args["termFinancialId"]
        uid = args["userId"]
        pwd = args["passWord"]
        exp = args["exp"]

        send = Send()
        send.send_login(session, name, password)

        result = send.send_buyc(session, tid, uid, pwd)

        assert result["code"] == 200 and result["body"]["code"] == exp


class Test_buyf:
    @pytest.mark.parametrize("args", read_yaml("../data/buyf.yaml"))
    def test_buyc(self, args):
        session = requests.session()

        name = args["username"]
        password = args["password"]

        fid = args["fundproductId"]
        uid = args["userId"]
        pwd = args["passWord"]
        exp=args["exp"]

        send = Send()
        send.send_login(session, name, password)

        result = send.send_buyc(session, fid, uid, pwd)

        assert result["code"] == 200 and result["body"]["code"] == exp

class Test_loan:
    @pytest.mark.parametrize("args", read_yaml("../data/loan.yaml"))
    def test_loan(self,args):
        session=requests.session()

        name=args["username"]
        password=args["password"]

        amount=args["amount"]
        term=args["term"]
        rate=args["rate"]
        exp=args["exp"]

        send=Send()

        send.send_login(session, name, password)

        result=send.send_loan(session, amount, term, rate)

        assert result["code"]==200 and result["body"]["code"]==exp